package info.sean.main;

import org.apache.commons.lang3.time.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

public class Main {
    /**
     * SLF4J日志框架提供了一个MDC(Mapped Diagnostic Contexts)工具类，谷歌翻译为映射的诊断上下文
     */

    private static final String KEY = "requestId";
    private static final Logger logger = LoggerFactory.getLogger(Main.class);

    public static void main(String[] args) throws ParseException {

        // 入口传入请求ID
        MDC.put(KEY, UUID.randomUUID().toString());

        // 打印日志
        logger.debug("log in main thread 1");
        logger.debug("log in main thread 2");
        logger.debug("log in main thread 3");

        // 出口移除请求ID
        MDC.remove(KEY);

        dateTruncateForDay(new Date());
        System.out.println("\"-----------------------\" = " + "-----------------------");
        String shareCode = createShareCode();
        System.out.println("shareCode = " + shareCode);

        int abs = Math.abs(-1);
        System.out.println("abs = " + abs);
        System.out.println("Math.abs(Integer.MIN_VALUE) = " + Math.abs(Integer.MIN_VALUE));
        System.out.println("Math.abs(Long.MIN_VALUE) = " + Math.abs(Long.MIN_VALUE));
        System.out.println("Math.abs((long)Integer.MIN_VALUE) = " + Math.abs((long) Integer.MIN_VALUE));
        System.out.println("Math.abs((double)Long.MIN_VALUE) = " + Math.abs((double) Long.MIN_VALUE));
        DateFormat dateFormat2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Integer startOverdueDay =180;
        Integer endOverdueDay = null;

//        if (endOverdueDay == null){
//            endOverdueDay = startOverdueDay;
//        }
        System.out.println("endOverdueDay = " + endOverdueDay);


        Date minMustPayDate = null;
        Date maxMustPayDate  = null;
        //获取逾期最大账单日和最小账单日
        if (endOverdueDay == null){
            minMustPayDate = dateFormat2.parse("2022-12-13 00:31:01");
            maxMustPayDate = dateFormat2.parse("2022-12-14 00:31:01");
        }else {
            minMustPayDate = DateUtils.addDays(new Date(),-endOverdueDay);
            maxMustPayDate = DateUtils.addDays(new Date(),-startOverdueDay);
        }

        List<Date> mustPayDates = getBetweenDates(minMustPayDate, maxMustPayDate);
        for (Date mustPayDate : mustPayDates) {
            System.out.println("mustPayDate = " + mustPayDate);
        }
    }


    public static void dateTruncateForDay(Date params) {

        if ( params  != null) {
            System.out.println("DateUtils.truncate( params, Calendar.DAY_OF_MONTH) = " + DateUtils.truncate(params, Calendar.DAY_OF_MONTH));
        }
    }
    /**
     * 获取两个日期之间的所有日期(包涵begin,end)
     */
    public static List<Date> getBetweenDates(Date begin, Date end) {
        List<Date> result = new ArrayList<>();
        Calendar tempStart = Calendar.getInstance();
        tempStart.setTime(begin);
        while(begin.getTime()<=end.getTime()){
            result.add(tempStart.getTime());
            tempStart.add(Calendar.DAY_OF_MONTH, 1);
            begin = tempStart.getTime();
        }
        return result;
    }


    /**
     * 生成邀请码
     *
     * @return
     */
    public static String createShareCode() {
        int maxNum = 36;
        int i;
        int count = 0;
        char[] str = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W',
                'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
        StringBuffer pwd = new StringBuffer("");
        Random r = new Random();
        while (count < 10) {
            int nextInt = r.nextInt(maxNum);
            i = Math.abs(nextInt);
            if (i >= 0 && i < str.length) {
                pwd.append(str[i]);
                count++;
            }
        }
        return pwd.toString();
    }

}
